package com.itheima.sfbx.insurance.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sfbx.framework.commons.exception.ProjectException;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.framework.commons.utils.EmptyUtil;
import com.itheima.sfbx.framework.commons.utils.ExceptionsUtil;
import com.itheima.sfbx.insurance.constant.InsuranceCacheConstant;
import com.itheima.sfbx.insurance.constant.InsuranceConditionCacheConstant;
import com.itheima.sfbx.insurance.dto.InsuranceConditionVO;
import com.itheima.sfbx.insurance.enums.InsuranceConditionEnum;
import com.itheima.sfbx.insurance.mapper.InsuranceConditionMapper;
import com.itheima.sfbx.insurance.pojo.InsuranceCondition;
import com.itheima.sfbx.insurance.pojo.InsurancePlan;
import com.itheima.sfbx.insurance.service.IInsuranceConditionService;
import com.itheima.sfbx.insurance.service.IInsuranceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description：保险筛选项服务实现类
 */
@Slf4j
@Service
public class InsuranceConditionServiceImpl extends ServiceImpl<InsuranceConditionMapper, InsuranceCondition> implements IInsuranceConditionService {

    @Autowired
    private IInsuranceService insuranceService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean save(List<InsuranceConditionVO> vos) {
        List<InsuranceCondition> list = BeanConv.toBeanList(vos,InsuranceCondition.class);
        return this.saveBatch(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(List<InsuranceConditionVO> vos) {
        //先删再增
        Long insuranceId = Long.parseLong(vos.get(0).getInsuranceId());
        remove(new LambdaQueryWrapper<InsuranceCondition>().eq(InsuranceCondition::getInsuranceId,insuranceId));
        List<InsuranceCondition> conditions = BeanConv.toBeanList(vos,InsuranceCondition.class);
        return saveBatch(conditions);
    }


    @Override
    public List<InsuranceConditionVO> findList(InsuranceConditionVO vo) {
        List<InsuranceCondition> list = lambdaQuery()
                .eq(StrUtil.isNotBlank(vo.getInsuranceId()), InsuranceCondition::getInsuranceId, vo.getInsuranceId())
                .eq(StrUtil.isNotBlank(vo.getConditionKey()), InsuranceCondition::getConditionKey, vo.getConditionKey())
                .eq(StrUtil.isNotBlank(vo.getDataState()), InsuranceCondition::getDataState, vo.getDataState())
                .like(StrUtil.isNotBlank(vo.getConditionKeyName()), InsuranceCondition::getConditionKeyName, vo.getConditionKeyName())
                .list();
        return BeanConv.toBeanList(list,InsuranceConditionVO.class);
    }

}
